package com.neuedu.his.dao;


import com.neuedu.his.common.utils.DBUtils;
import com.neuedu.his.entity.InspectApply;

import java.util.List;
import java.util.Map;

public class InspectApplyDao {

    /**
     * 查询集合
     */
    public List<InspectApply> selectList(String where , Object ... params){
        String sql =" select * from inspect_apply ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        List<InspectApply> inspectApplys = DBUtils.selectList(InspectApply.class, sql, params);

        return inspectApplys;
    }

    /**
     * 分页查询
     * @param where
     * @param pageNo
     * @param size
     * @param params
     * @return
     */
    public List<InspectApply> selectPage(String where , int pageNo,int size ,Object ... params){
        String sql =" select * from inspect_apply ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql += " limit "+(pageNo-1)*size+"   ,"+size;

        System.out.println("sql = " + sql);
        List<InspectApply> inspectApplys = DBUtils.selectList(InspectApply.class, sql, params);

        return inspectApplys;
    }


    /**
     * 查询单条结果
     */
    public InspectApply selectOne(String where ,Object ... params){
        String sql =" select * from inspect_apply ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(InspectApply.class, sql, params);
    }


    /**
     * 查询总条数
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from inspect_apply ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }




    /**
     * 根据数据进行插入
     * @param inspectApply
     * @return
     */
    public int insert(InspectApply inspectApply){
        String sql = "insert into inspect_apply(       inspect_id   ,  name,      price   ,       regist_id   ,       result   ,       status   ) values(   ?   ,     ?   ,  ?,  ?   ,     ?   ,     ?    )";
        return DBUtils.update( sql,    inspectApply.getInspectId()    ,  inspectApply.getName(),   inspectApply.getPrice()    ,     inspectApply.getRegistId()    ,     inspectApply.getResult()    ,     inspectApply.getStatus() );
    }


    /**
     * 根据主键更新一条
     * @param inspectApply
     * @return
     */
    public int updateById(InspectApply inspectApply){
        String sql = " update inspect_apply set     inspect_id = ?   ,    name = ?   ,        price = ?   ,       regist_id = ?   ,       result = ?   ,       status = ?   where  item_id = ?  ";

        return DBUtils.update( sql,
                inspectApply.getInspectId()  ,
                inspectApply.getName()  ,
                inspectApply.getPrice()  ,
                inspectApply.getRegistId()  ,
                inspectApply.getResult()  ,
                inspectApply.getStatus()  ,
                inspectApply.getItemId() );
    }



    /**
     * 根据主键更新一条
     * @param id
     * @return
     */
    public int deleteById(Integer id){
        String sql = " delete from inspect_apply  where item_id = ?";
        return DBUtils.update( sql, id);

    }




    public int updateByCondation(String status  ,List<Integer> param){

        StringBuffer sql = new  StringBuffer(" update inspect_apply set     status = ? where item_id in ( ");
        for (Integer id : param) {
            sql.append( " ?," );
        }
        sql.deleteCharAt(sql.length() - 1 );//多于的 逗号删掉
        sql.append(")");


        Object[] paras = new Object[param.size() +1];
        paras[0] = status;
        for(int i=0;i<param.size();i++){
            paras[i+1] = param.get(i);
        }

        return DBUtils.update( sql.toString(), paras);
    }

}